//
//  CyCStandardCell.m
//  Cycling
//
//  Created by cjg on 2023/3/12.
//

#import "CyCStandardCell.h"

@interface CyCStandardCell ()

@property (nonatomic , strong) UIView *shadeView;

@property (nonatomic, weak) UILabel *numLbl;

@end


@implementation CyCStandardCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        self.isUserEnable = YES;
    }
    return self;
}

- (void)setupUI{
    self.imView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imView];
    [self.imView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(MARGIN_MIDDLE);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(MARGIN_MAX, MARGIN_MAX));
    }];

    self.titleLB = [[UILabel alloc] init];
    self.titleLB.font = FONT_SECONDARY_TITLE;
    self.titleLB.textColor = COLOR_BLACK;
    self.titleLB.adjustsFontSizeToFitWidth = YES;
    self.titleLB.minimumScaleFactor = 0.6;
    [self.contentView addSubview:self.titleLB];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(MARGIN_MIDDLE);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(30);
    }];
    
    self.subTitleLB = [[UILabel alloc] init];
    self.subTitleLB.font = FONT_BODY_16;
    self.subTitleLB.textColor = COLOR_SIMPLEBLACK;
    self.subTitleLB.textAlignment = NSTextAlignmentRight;
    [self.subTitleLB setContentHuggingPriority:UILayoutPriorityRequired + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [self.subTitleLB setContentCompressionResistancePriority:UILayoutPriorityRequired + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [self.contentView addSubview:self.subTitleLB];
    [self.subTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        make.left.greaterThanOrEqualTo(self.titleLB.mas_right).offset(5);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(30);
    }];
    
    self.selectImView = [[UIImageView alloc] init];
    self.selectImView.image = [UIImage imageNamed:@"icon_not_select"];
    [self.contentView addSubview:self.selectImView];
    [self.selectImView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(MARGIN_MAX, MARGIN_MAX));
    }];
    
    self.arrowIMView = [[UIImageView alloc] init];
    self.arrowIMView.image = [UIImage imageNamed:@"arrow"];
    [self.contentView addSubview:self.arrowIMView];
    [self.arrowIMView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(MARGIN_MAX, MARGIN_MAX));
    }];

    self.shadeView = [[UIView alloc] init];
    self.shadeView.backgroundColor = COLOR_SHADE;
    [self.contentView addSubview:self.shadeView];
    [self.shadeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView).offset(0);
    }];
    self.shadeView.hidden = YES;

}

-(void)setCellType:(CycStandardCellType)cellType {
    _cellType = cellType;
    
    if (cellType == CycStandardCellTypeDefult) {
        self.imView.hidden = NO;
        self.selectImView.hidden = YES;
        self.arrowIMView.hidden = NO;
        [self.imView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        }];
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MIDDLE+MARGIN_MAX+MARGIN_MAX);
        }];
        [self.arrowIMView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        }];
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX*2);
        }];
    }
    if (cellType == CycStandardCellTypeTitle) {
        self.imView.hidden = YES;
        self.selectImView.hidden = YES;
        self.arrowIMView.hidden = YES;
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        }];
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        }];
    }

    if (cellType == CycStandardCellTypeArrow) {
        self.imView.hidden = YES;
        self.selectImView.hidden = YES;
        self.arrowIMView.hidden = NO;
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        }];
        [self.arrowIMView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
        }];
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MIN-MARGIN_MAX-MARGIN_MAX);
        }];
    }
    if (cellType == CycStandardCellTypeSelect) {
        self.imView.hidden = YES;
        self.selectImView.hidden = NO;
        self.arrowIMView.hidden = YES;
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        }];
        [self.selectImView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX-1);
        }];
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MIN-MARGIN_MAX-MARGIN_MAX);
        }];
    }
    if (cellType == CycStandardCellTypeSelectAndIM) {
        self.imView.hidden = NO;
        self.selectImView.hidden = NO;
        self.arrowIMView.hidden = YES;
        [self.imView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        }];
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MIDDLE+MARGIN_MAX+MARGIN_MAX);
        }];
        [self.selectImView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MAX-1);
        }];
        [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-MARGIN_MIN-MARGIN_MAX-MARGIN_MAX);
        }];
    }
}
- (void)setIsUserEnable:(BOOL)isUserEnable {
    _isUserEnable = isUserEnable;
    self.shadeView.hidden = isUserEnable;
    self.userInteractionEnabled = isUserEnable;
}

-(void)setIsChoose:(BOOL)isChoose {
    _isChoose = isChoose;
    if (isChoose) {
        self.selectImView.image = [UIImage imageNamed:@"icon_select"];
    }else {
        self.selectImView.image = [UIImage imageNamed:@"icon_not_select"];
    }
}

- (void)showTryrideNum:(NSInteger)num
{
    if(self.numLbl) {
        [self.numLbl.superview removeFromSuperview];
    }
    if(num == 0) {
        return;
    }
    [self numContentViewSetup];
    self.numLbl.text = [NSString stringWithFormat:@"%@", @(num)];
}

- (void)showApplicationVersion
{
    if(self.numLbl) {
        [self.numLbl.superview removeFromSuperview];
    }
    [self numContentViewSetupRemoveArrowView:YES];
    self.numLbl.text = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
}

- (void)numContentViewSetup
{
    [self numContentViewSetupRemoveArrowView:NO];
}

- (void)numContentViewSetupRemoveArrowView:(BOOL)willRemove
{
    if(self.numLbl.window) {
        return;
    }
    
    UIView *contentView = [UIView new];
    if(willRemove) {
        contentView.backgroundColor = [UIColor whiteColor];
    } else {
        contentView.backgroundColor = [UIColor redColor];
    }
    contentView.layer.cornerRadius = 4;
    contentView.clipsToBounds = YES;
    
    [self.contentView addSubview:contentView];
    if(self.arrowIMView) {
        if(willRemove) {
            self.arrowIMView.hidden = YES;
            [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-MARGIN_MAX);
                make.centerY.equalTo(self.contentView);
                make.left.mas_greaterThanOrEqualTo(self.titleLB.mas_right).offset(MARGIN_MIDDLE);
            }];
        } else {
            self.arrowIMView.hidden = NO;
            [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.arrowIMView.mas_left).offset(-2);
                make.centerY.equalTo(self.contentView);
                make.left.mas_greaterThanOrEqualTo(self.titleLB.mas_right).offset(MARGIN_MIDDLE);
            }];
        }
    } else {
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-MARGIN_MAX);
            make.centerY.equalTo(self.contentView);
            make.left.mas_greaterThanOrEqualTo(self.titleLB.mas_right).offset(MARGIN_MIDDLE);
        }];
    }
    UILabel *numLabel = [UILabel new];
    if(willRemove) {
        numLabel.textColor = [UIColor blackColor];
    } else {
        numLabel.textColor = [UIColor whiteColor];
    }
    
    numLabel.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:numLabel];
    self.numLbl = numLabel;
    
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(3, 5, 3, 5));
    }];
    
}

@end
